package cn.tdzk;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import optimal.response.ResponseOptDay;
import optimal.service.impl.OptimalDay;
import org.json.JSONObject;
import southernGrid.CBLRS;
import southernGrid.ISS;
import southernGrid.OpenTower;
import tool.ArrayExtend;

public class AlgorithmOptimalDay {
    public void doOptimalDay(JSONObject data) {
        int N_DOCU = data.getInt("N_DOCU");
        String DOCU_Form = data.getString("DOCU_Form");
        double nominalCoolingLoad_DOCU = data.getDouble("nominalCoolingLoad_DOCU");
        double nominalCoolingElectricPower_DOCU = data.getDouble("nominalCoolingElectricPower_DOCU");
        double nominalCoolingTin_load_DOCU = data.getDouble("nominalCoolingTin_load_DOCU");
        double nominalCoolingTout_load_DOCU = data.getDouble("nominalCoolingTout_load_DOCU");
        double nominalCoolingTin_source_DOCU = data.getDouble("nominalCoolingTin_source_DOCU");
        double nominalCoolingTout_source_DOCU = data.getDouble("nominalCoolingTout_source_DOCU");
        double[] cooling_Parameter_DOCU1 = DataConverter.toDoubleArray(data.getString("cooling_Parameter_DOCU1"));
        double[] cooling_Parameter_DOCU2 = DataConverter.toDoubleArray(data.getString("cooling_Parameter_DOCU2"));
        double[] cooling_Parameter_DOCU3 = DataConverter.toDoubleArray(data.getString("cooling_Parameter_DOCU3"));
        double[] cooling_Parameter_DOCU4 = DataConverter.toDoubleArray(data.getString("cooling_Parameter_DOCU4"));
        double coolingTout_Set_min_DOCU = data.getDouble("coolingTout_Set_min_DOCU");
        double coolingTout_Set_max_DOCU = data.getDouble("coolingTout_Set_max_DOCU");
        double nominalIceMakingLoad_DOCU = data.getDouble("nominalIceMakingLoad_DOCU");
        double nominalIceMakingElectricPower_DOCU = data.getDouble("nominalIceMakingElectricPower_DOCU");
        double nominalIceMakingTin_load_DOCU = data.getDouble("nominalIceMakingTin_load_DOCU");
        double nominalIceMakingTout_load_DOCU = data.getDouble("nominalIceMakingTout_load_DOCU");
        double nominalIceMakingTin_source_DOCU = data.getDouble("nominalIceMakingTin_source_DOCU");
        double nominalIceMakingTout_source_DOCU = data.getDouble("nominalIceMakingTout_source_DOCU");
        double[] IceMaking_Parameter_DOCU1 = DataConverter.toDoubleArray(data.getString("IceMaking_Parameter_DOCU1"));
        double[] IceMaking_Parameter_DOCU2 = DataConverter.toDoubleArray(data.getString("IceMaking_Parameter_DOCU2"));
        double[] IceMaking_Parameter_DOCU3 = DataConverter.toDoubleArray(data.getString("IceMaking_Parameter_DOCU3"));
        double[] IceMaking_Parameter_DOCU4 = DataConverter.toDoubleArray(data.getString("IceMaking_Parameter_DOCU4"));
        double IceMakingTout_Set_min_DOCU = data.getDouble("IceMakingTout_Set_min_DOCU");
        double IceMakingTout_Set_max_DOCU = data.getDouble("IceMakingTout_Set_max_DOCU");
        int N_IceStorageTank_ISS = data.getInt("N_IceStorageTank_ISS");
        double iceStorageCapacity_IceStorageTank_ISS = data.getDouble("iceStorageCapacity_IceStorageTank_ISS");
        double[] geometricDimensioning_IceStorageTank_ISS = DataConverter.toDoubleArray(data.getString("geometricDimensioning_IceStorageTank_ISS"));
        String pipeType_IceStorageTank_ISS = data.getString("pipeType_IceStorageTank_ISS");
        int N_tubePasses_IceStorageTank_ISS = data.getInt("N_tubePasses_IceStorageTank_ISS");
        int N_channels_everyPass_IceStorageTank_ISS = data.getInt("N_channels_everyPass_IceStorageTank_ISS");
        double Din_tube_IceStorageTank_ISS = data.getDouble("Din_tube_IceStorageTank_ISS");
        double Dout_tube_IceStorageTank_ISS = data.getDouble("Dout_tube_IceStorageTank_ISS");
        double Dout_corrugatedTube_IceStorageTank_ISS = data.getDouble("Dout_corrugatedTube_IceStorageTank_ISS");
        double nominalChargeTin_IceStorageTank_ISS = data.getDouble("nominalChargeTin_IceStorageTank_ISS");
        double nominalChargeTout_IceStorageTank_ISS = data.getDouble("nominalChargeTout_IceStorageTank_ISS");
        double nominalChargeFlow_IceStorageTank_ISS = data.getDouble("nominalChargeFlow_IceStorageTank_ISS");
        double nominalDisChargeTin_IceStorageTank_ISS = data.getDouble("nominalDisChargeTin_IceStorageTank_ISS");
        double nominalDisChargeTout_IceStorageTank_ISS = data.getDouble("nominalDisChargeTout_IceStorageTank_ISS");
        double nominalDisChargeFlow_IceStorageTank_ISS = data.getDouble("nominalDisChargeFlow_IceStorageTank_ISS");
        double[] charge_Parameter_IceStorageTank_ISS = DataConverter.toDoubleArray(data.getString("charge_Parameter_IceStorageTank_ISS"));
        double[] discharge_Parameter_IceStorageTank_ISS = DataConverter.toDoubleArray(data.getString("discharge_Parameter_IceStorageTank_ISS"));
        double baseChargeLoad = data.getDouble("baseChargeLoad");
        double baseDisChargeLoad = data.getDouble("baseDisChargeLoad");
        int N_plateHeatExchanger_ISS = data.getInt("N_plateHeatExchanger_ISS");
        double exchangeArea_plateHeatExchanger_ISS = data.getDouble("exchangeArea_plateHeatExchanger_ISS");
        double nominalPrimarySideTin_plateHeatExchanger_ISS = data.getDouble("nominalPrimarySideTin_plateHeatExchanger_ISS");
        double nominalPrimarySideTout_plateHeatExchanger_ISS = data.getDouble("nominalPrimarySideTout_plateHeatExchanger_ISS");
        double nominalPrimarySideFlow_plateHeatExchanger_ISS = data.getDouble("nominalPrimarySideFlow_plateHeatExchanger_ISS");
        double nominalSecondarySideTin_plateHeatExchanger_ISS = data.getDouble("nominalSecondarySideTin_plateHeatExchanger_ISS");
        double nominalSecondarySideTout_plateHeatExchanger_ISS = data.getDouble("nominalSecondarySideTout_plateHeatExchanger_ISS");
        double nominalSecondarySideFlow_plateHeatExchanger_ISS = data.getDouble("nominalSecondarySideFlow_plateHeatExchanger_ISS");
        double[] heatTransfer_Parameter_plateHeatExchanger_ISS = DataConverter.toDoubleArray(data.getString("heatTransfer_Parameter_plateHeatExchanger_ISS"));
        int N_User1Pump_ISS = data.getInt("N_User1Pump_ISS");
        double nominalFlow_User1Pump_ISS = data.getDouble("nominalFlow_User1Pump_ISS");
        double nominalHead_User1Pump_ISS = data.getDouble("nominalHead_User1Pump_ISS");
        double minFrequency_User1Pump_ISS = data.getDouble("minFrequency_User1Pump_ISS");
        double maxFrequency_User1Pump_ISS = data.getDouble("maxFrequency_User1Pump_ISS");
        double[] flowHead_User1Pump_Parameter_ISS = DataConverter.toDoubleArray(data.getString("flowHead_User1Pump_Parameter_ISS"));
        double[] flowEfficiency_User1Pump_Parameter_ISS = DataConverter.toDoubleArray(data.getString("flowEfficiency_User1Pump_Parameter_ISS"));
        double[] flow_User1Pump_ISS = DataConverter.toDoubleArray(data.getString("flow_User1Pump_ISS"));
        int N_SourcePump_ISS = data.getInt("N_SourcePump_ISS");
        double nominalFlow_SourcePump_ISS = data.getDouble("nominalFlow_SourcePump_ISS");
        double nominalHead_SourcePump_ISS = data.getDouble("nominalHead_SourcePump_ISS");
        double minFrequency_SourcePump_ISS = data.getDouble("minFrequency_SourcePump_ISS");
        double maxFrequency_SourcePump_ISS = data.getDouble("maxFrequency_SourcePump_ISS");
        double[] flowHead_SourcePump_Parameter_ISS = DataConverter.toDoubleArray(data.getString("flowHead_SourcePump_Parameter_ISS"));
        double[] flowEfficiency_SourcePump_Parameter_ISS = DataConverter.toDoubleArray(data.getString("flowEfficiency_SourcePump_Parameter_ISS"));
        double[] flow_SourcePump_ISS = DataConverter.toDoubleArray(data.getString("flow_SourcePump_ISS"));
        int N_EthylAlcoholPump_ISS = data.getInt("N_EthylAlcoholPump_ISS");
        double nominalFlow_EthylAlcoholPump_ISS = data.getDouble("nominalFlow_EthylAlcoholPump_ISS");
        double nominalHead_EthylAlcoholPump_ISS = data.getDouble("nominalHead_EthylAlcoholPump_ISS");
        double minFrequency_EthylAlcoholPump_ISS = data.getDouble("minFrequency_EthylAlcoholPump_ISS");
        double maxFrequency_EthylAlcoholPump_ISS = data.getDouble("maxFrequency_EthylAlcoholPump_ISS");
        double[] flowHead_EthylAlcoholPump_Parameter_ISS = DataConverter.toDoubleArray(data.getString("flowHead_EthylAlcoholPump_Parameter_ISS"));
        double[] flowEfficiency_EthylAlcoholPump_Parameter_ISS = DataConverter.toDoubleArray(data.getString("flowEfficiency_EthylAlcoholPump_Parameter_ISS"));
        double[] flow_EthylAlcoholPump_ISS = DataConverter.toDoubleArray(data.getString("flow_EthylAlcoholPump_ISS"));
        ISS iss = new ISS(N_DOCU, DOCU_Form, nominalCoolingLoad_DOCU, nominalCoolingElectricPower_DOCU, nominalCoolingTin_load_DOCU, nominalCoolingTout_load_DOCU, nominalCoolingTin_source_DOCU, nominalCoolingTout_source_DOCU, cooling_Parameter_DOCU1, cooling_Parameter_DOCU2, cooling_Parameter_DOCU3, cooling_Parameter_DOCU4, coolingTout_Set_min_DOCU, coolingTout_Set_max_DOCU, nominalIceMakingLoad_DOCU, nominalIceMakingElectricPower_DOCU, nominalIceMakingTin_load_DOCU, nominalIceMakingTout_load_DOCU, nominalIceMakingTin_source_DOCU, nominalIceMakingTout_source_DOCU, IceMaking_Parameter_DOCU1, IceMaking_Parameter_DOCU2, IceMaking_Parameter_DOCU3, IceMaking_Parameter_DOCU4, IceMakingTout_Set_min_DOCU, IceMakingTout_Set_max_DOCU, N_IceStorageTank_ISS, iceStorageCapacity_IceStorageTank_ISS, geometricDimensioning_IceStorageTank_ISS, pipeType_IceStorageTank_ISS, N_tubePasses_IceStorageTank_ISS, N_channels_everyPass_IceStorageTank_ISS, Din_tube_IceStorageTank_ISS, Dout_tube_IceStorageTank_ISS, Dout_corrugatedTube_IceStorageTank_ISS, nominalChargeTin_IceStorageTank_ISS, nominalChargeTout_IceStorageTank_ISS, nominalChargeFlow_IceStorageTank_ISS, nominalDisChargeTin_IceStorageTank_ISS, nominalDisChargeTout_IceStorageTank_ISS, nominalDisChargeFlow_IceStorageTank_ISS, charge_Parameter_IceStorageTank_ISS, discharge_Parameter_IceStorageTank_ISS, baseChargeLoad, baseDisChargeLoad, N_plateHeatExchanger_ISS, exchangeArea_plateHeatExchanger_ISS, nominalPrimarySideTin_plateHeatExchanger_ISS, nominalPrimarySideTout_plateHeatExchanger_ISS, nominalPrimarySideFlow_plateHeatExchanger_ISS, nominalSecondarySideTin_plateHeatExchanger_ISS, nominalSecondarySideTout_plateHeatExchanger_ISS, nominalSecondarySideFlow_plateHeatExchanger_ISS, heatTransfer_Parameter_plateHeatExchanger_ISS, N_User1Pump_ISS, nominalFlow_User1Pump_ISS, nominalHead_User1Pump_ISS, minFrequency_User1Pump_ISS, maxFrequency_User1Pump_ISS, flowHead_User1Pump_Parameter_ISS, flowEfficiency_User1Pump_Parameter_ISS, flow_User1Pump_ISS, N_SourcePump_ISS, nominalFlow_SourcePump_ISS, nominalHead_SourcePump_ISS, minFrequency_SourcePump_ISS, maxFrequency_SourcePump_ISS, flowHead_SourcePump_Parameter_ISS, flowEfficiency_SourcePump_Parameter_ISS, flow_SourcePump_ISS, N_EthylAlcoholPump_ISS, nominalFlow_EthylAlcoholPump_ISS, nominalHead_EthylAlcoholPump_ISS, minFrequency_EthylAlcoholPump_ISS, maxFrequency_EthylAlcoholPump_ISS, flowHead_EthylAlcoholPump_Parameter_ISS, flowEfficiency_EthylAlcoholPump_Parameter_ISS, flow_EthylAlcoholPump_ISS);
        int N_BLRU = data.getInt("N_BLRU");
        String BLRU_Form = data.getString("BLRU_Form");
        double nominalCoolingLoad_BLRU = data.getDouble("nominalCoolingLoad_BLRU");
        double nominalCoolingElectricPower_BLRU = data.getDouble("nominalCoolingElectricPower_BLRU");
        double nominalCoolingTin_load_BLRU = data.getDouble("nominalCoolingTin_load_BLRU");
        double nominalCoolingTout_load_BLRU = data.getDouble("nominalCoolingTout_load_BLRU");
        double nominalCoolingTin_source_BLRU = data.getDouble("nominalCoolingTin_source_BLRU");
        double nominalCoolingTout_source_BLRU = data.getDouble("nominalCoolingTout_source_BLRU");
        double[] cooling_Parameter_BLRU1 = DataConverter.toDoubleArray(data.getString("cooling_Parameter_BLRU1"));
        double[] cooling_Parameter_BLRU2 = DataConverter.toDoubleArray(data.getString("cooling_Parameter_BLRU2"));
        double[] cooling_Parameter_BLRU3 = DataConverter.toDoubleArray(data.getString("cooling_Parameter_BLRU3"));
        double[] cooling_Parameter_BLRU4 = DataConverter.toDoubleArray(data.getString("cooling_Parameter_BLRU4"));
        double coolingTout_Set_min_BLRU = data.getDouble("coolingTout_Set_min_BLRU");
        double coolingTout_Set_max_BLRU = data.getDouble("coolingTout_Set_max_BLRU");
        int N_User1Pump_CBLRS = data.getInt("N_User1Pump_CBLRS");
        double nominalFlow_User1Pump_CBLRS = data.getDouble("nominalFlow_User1Pump_CBLRS");
        double nominalHead_User1Pump_CBLRS = data.getDouble("nominalHead_User1Pump_CBLRS");
        double minFrequency_User1Pump_CBLRS = data.getDouble("minFrequency_User1Pump_CBLRS");
        double maxFrequency_User1Pump_CBLRS = data.getDouble("maxFrequency_User1Pump_CBLRS");
        double[] flowHead_User1Pump_Parameter_CBLRS = DataConverter.toDoubleArray(data.getString("flowHead_User1Pump_Parameter_CBLRS"));
        double[] flowEfficiency_User1Pump_Parameter_CBLRS = DataConverter.toDoubleArray(data.getString("flowEfficiency_User1Pump_Parameter_CBLRS"));
        double[] flow_User1Pump_CBLRS = DataConverter.toDoubleArray(data.getString("flow_User1Pump_CBLRS"));
        int N_SourcePump_CBLRS = data.getInt("N_SourcePump_CBLRS");
        double nominalFlow_SourcePump_CBLRS = data.getDouble("nominalFlow_SourcePump_CBLRS");
        double nominalHead_SourcePump_CBLRS = data.getDouble("nominalHead_SourcePump_CBLRS");
        double minFrequency_SourcePump_CBLRS = data.getDouble("minFrequency_SourcePump_CBLRS");
        double maxFrequency_SourcePump_CBLRS = data.getDouble("maxFrequency_SourcePump_CBLRS");
        double[] flowHead_SourcePump_Parameter_CBLRS = DataConverter.toDoubleArray(data.getString("flowHead_SourcePump_Parameter_CBLRS"));
        double[] flowEfficiency_SourcePump_Parameter_CBLRS = DataConverter.toDoubleArray(data.getString("flowEfficiency_SourcePump_Parameter_CBLRS"));
        double[] flow_SourcePump_CBLRS = DataConverter.toDoubleArray(data.getString("flow_SourcePump_CBLRS"));
        CBLRS cblrs = new CBLRS(N_BLRU, BLRU_Form, nominalCoolingLoad_BLRU, nominalCoolingElectricPower_BLRU, nominalCoolingTin_load_BLRU, nominalCoolingTout_load_BLRU, nominalCoolingTin_source_BLRU, nominalCoolingTout_source_BLRU, cooling_Parameter_BLRU1, cooling_Parameter_BLRU2, cooling_Parameter_BLRU3, cooling_Parameter_BLRU4, coolingTout_Set_min_BLRU, coolingTout_Set_max_BLRU, N_User1Pump_CBLRS, nominalFlow_User1Pump_CBLRS, nominalHead_User1Pump_CBLRS, minFrequency_User1Pump_CBLRS, maxFrequency_User1Pump_CBLRS, flowHead_User1Pump_Parameter_CBLRS, flowEfficiency_User1Pump_Parameter_CBLRS, flow_User1Pump_CBLRS, N_SourcePump_CBLRS, nominalFlow_SourcePump_CBLRS, nominalHead_SourcePump_CBLRS, minFrequency_SourcePump_CBLRS, maxFrequency_SourcePump_CBLRS, flowHead_SourcePump_Parameter_CBLRS, flowEfficiency_SourcePump_Parameter_CBLRS, flow_SourcePump_CBLRS);
        int N_OpenTower = data.getInt("N_OpenTower");
        double nominalLoad_OpenTower = data.getDouble("nominalLoad_OpenTower");
        double nominalFlow_OpenTower = data.getDouble("nominalFlow_OpenTower");
        double nominal_a_OpenTower = data.getDouble("nominal_a_OpenTower");
        double nominal_Ts_OpenTower = data.getDouble("nominal_Ts_OpenTower");
        double nominalPower_OpenTower = data.getDouble("nominalPower_OpenTower");
        double minFrequency_OpenTower = data.getDouble("minFrequency_OpenTower");
        double maxFrequency_OpenTower = data.getDouble("maxFrequency_OpenTower");
        double[] power_OpenTower_Parameter = DataConverter.toDoubleArray(data.getString("power_OpenTower_Parameter"));
        OpenTower openTower = new OpenTower(N_OpenTower, nominalLoad_OpenTower, nominalFlow_OpenTower, nominal_a_OpenTower, nominal_Ts_OpenTower, nominalPower_OpenTower, minFrequency_OpenTower, maxFrequency_OpenTower, power_OpenTower_Parameter);
        String operationModel = data.getString("operationModel");
        String optimizationModel_day = data.getString("optimizationModel_day");
        double coolingTout_load_BLRU = data.getDouble("coolingTout_load_BLRU");
        double coolingTout_load_DOCU = data.getDouble("coolingTout_load_DOCU");
        double iceMakingTout_load_DOCU = data.getDouble("iceMakingTout_load_DOCU");
        String economicParameterModel = data.getString("economicParameterModel");
        double economicSingleElectricityPrice = data.getDouble("economicSingleElectricityPrice");
        double[] economicTimeSharingElectricityPrice = DataConverter.toDoubleArray(data.getString("economicTimeSharingElectricityPrice"));
        int priceElecUnit = data.getInt("priceElecUnit");
        double economicWaterPrice = data.getDouble("economicWaterPrice");
        String carbonParameterModel = data.getString("carbonParameterModel");
        double carbonParameterFixedCarbonFactor = data.getDouble("carbonParameterFixedCarbonFactor");
        double[] carbonParameterDynamicCarbonFactor = DataConverter.toDoubleArray(data.getString("carbonParameterDynamicCarbonFactor"));

        ArrayList<Double> glycolInletTemperatures = Arrays.stream(DataConverter.toDoubleArray(data.getString("glycolInletTemperatures"))).boxed().collect(Collectors.toCollection(ArrayList::new));
        ArrayList<Double> glycolOutletTemperatures = Arrays.stream(DataConverter.toDoubleArray(data.getString("glycolOutletTemperatures"))).boxed().collect(Collectors.toCollection(ArrayList::new));
        ArrayList<Double> glycolFlows = Arrays.stream(DataConverter.toDoubleArray(data.getString("glycolFlows"))).boxed().collect(Collectors.toCollection(ArrayList::new));
        ArrayList<Double> iceStorageCapacities = Arrays.stream(DataConverter.toDoubleArray(data.getString("iceStorageCapacities"))).boxed().collect(Collectors.toCollection(ArrayList::new));
        ArrayList<Double> outdoorTemperatures = Arrays.stream(DataConverter.toDoubleArray(data.getString("outdoorTemperatures"))).boxed().collect(Collectors.toCollection(ArrayList::new));
        ArrayList<Double> outdoorHumidity = Arrays.stream(DataConverter.toDoubleArray(data.getString("outdoorHumidity"))).boxed().collect(Collectors.toCollection(ArrayList::new));
        ArrayList<Double> predictionLoads = Arrays.stream(DataConverter.toDoubleArray(data.getString("predictionLoads"))).boxed().collect(Collectors.toCollection(ArrayList::new));

        double simulatedDays = 1.0;
        double sumLoad = 0.0;
        double sumPower = 0.0;
        double sumPrice = 0.0;
        double sumLoadCBLRS = 0.0;
        double sumPowerCBLRS = 0.0;
        double sumPowerBLRU = 0.0;
        double sumPowerOT = 0.0;
        double sumLoadISS = 0.0;
        double sumPowerISS = 0.0;
        double sumLoadDOCUCooling = 0.0;
        double sumPowerDOCUCooling = 0.0;
        double sumLoadDOCUIceMaking = 0.0;
        double sumPowerDOCUIceMaking = 0.0;
        ArrayList<Double> sumLoadList = new ArrayList<>();
        ArrayList<Double> sumPowerList = new ArrayList<>();
        ArrayList<Double> sumPriceList = new ArrayList<>();
        int simulatedIndex = 1;

        ArrayList<Integer> breakdownBLRU = new ArrayList<>();
        ArrayList<Integer> breakdownUserPumpCBLRS = new ArrayList<>();
        ArrayList<Integer> breakdownSourcePumpCBLRS = new ArrayList<>();
        ArrayList<Integer> breakdownDOCU = new ArrayList<>();
        ArrayList<Integer> breakdownOT = new ArrayList<>();

        int[] lastActionDate = DataConverter.toIntArray(data.getString("lastActionDate"));
        System.out.println("***********************************************");
        ResponseOptDay responseOptDay = (new OptimalDay()).optimalDay(operationModel, optimizationModel_day, coolingTout_load_BLRU, coolingTout_load_DOCU, iceMakingTout_load_DOCU, economicParameterModel, economicTimeSharingElectricityPrice, priceElecUnit, economicWaterPrice, carbonParameterDynamicCarbonFactor, glycolInletTemperatures, glycolOutletTemperatures, glycolFlows, iceStorageCapacities, breakdownBLRU, breakdownUserPumpCBLRS, breakdownSourcePumpCBLRS, breakdownDOCU, breakdownOT, outdoorTemperatures, outdoorHumidity, predictionLoads, lastActionDate, cblrs, iss, openTower, simulatedIndex);
        System.out.println("第" + simulatedIndex + "次计算完成");
        ArrayList<Double> loadArrayList = responseOptDay.getCoolingLoadHourArray();
        ArrayList<Double> powerArrayList = responseOptDay.getCoolingPowerHourArray();
        sumLoad += ArrayExtend.arrayListDoubleSum(loadArrayList);
        sumPower += ArrayExtend.arrayListDoubleSum(powerArrayList);
        double dayPrice = 0.0;

        for (int i = 0; i < economicTimeSharingElectricityPrice.length; ++i) {
            dayPrice += economicTimeSharingElectricityPrice[i] * powerArrayList.get(i);
        }

        sumPrice += dayPrice;
        sumLoadList.add(sumLoad);
        sumPowerList.add(sumPower);
        sumPriceList.add(sumPrice);
        ArrayList<Double> loadArrayListCBLRS = responseOptDay.getCoolingLoadHourArrayCBLRS();
        ArrayList<Double> powerArrayListCBLRS = responseOptDay.getCoolingPowerHourArrayCBLRS();
        ArrayList<Double> powerArrayListBLRU = responseOptDay.getCoolingPowerHourArrayBLRU();
        ArrayList<Double> powerArrayListOT = responseOptDay.getPowerHourArrayOT();
        sumLoadCBLRS += ArrayExtend.arrayListDoubleSum(loadArrayListCBLRS);
        sumPowerCBLRS += ArrayExtend.arrayListDoubleSum(powerArrayListCBLRS);
        sumPowerBLRU += ArrayExtend.arrayListDoubleSum(powerArrayListBLRU);
        sumPowerOT += ArrayExtend.arrayListDoubleSum(powerArrayListOT);
        ArrayList<Double> loadArrayListISS = responseOptDay.getLoadHourArrayISS();
        ArrayList<Double> powerArrayListISS = responseOptDay.getPowerHourArrayISS();
        ArrayList<Double> coolingLoadArrayListDOCU = responseOptDay.getCoolingLoadHourArrayISS();
        ArrayList<Double> coolingPowerArrayListDOCU = responseOptDay.getCoolingPowerHourArrayDOCU();
        ArrayList<Double> iceMakingLoadArrayListDOCU = responseOptDay.getIceMakingLoadHourArrayISS();
        ArrayList<Double> iceMakingPowerArrayListDOCU = responseOptDay.getIceMakingPowerHourArrayDOCU();
        sumLoadISS += ArrayExtend.arrayListDoubleSum(loadArrayListISS);
        sumPowerISS += ArrayExtend.arrayListDoubleSum(powerArrayListISS);
        sumLoadDOCUCooling += ArrayExtend.arrayListDoubleSum(coolingLoadArrayListDOCU);
        sumPowerDOCUCooling += ArrayExtend.arrayListDoubleSum(coolingPowerArrayListDOCU);
        sumLoadDOCUIceMaking += ArrayExtend.arrayListDoubleSum(iceMakingLoadArrayListDOCU);
        sumPowerDOCUIceMaking += ArrayExtend.arrayListDoubleSum(iceMakingPowerArrayListDOCU);
        double sumEER = sumLoad / sumPower;
        double sumEER_CBLRS = sumLoadCBLRS / (sumPowerCBLRS + sumPowerOT);
        double sumCOP_BLRU = sumLoadCBLRS / sumPowerBLRU;
        double sumEER_ISS = sumLoadISS / sumPowerISS;
        double sumCOPDOCUCooling = sumLoadDOCUCooling / sumPowerDOCUCooling;
        double sumCOPDOCUIceMaking = sumLoadDOCUIceMaking / sumPowerDOCUIceMaking;

        try (BufferedWriter writer = new BufferedWriter(new FileWriter("consolidatedData/statisticalData.csv"))) {
            writer.write("sumLoadDay,sumPowerDay,sumPriceDay");
            writer.newLine();

            for (int i = 0; (double) i < simulatedDays; ++i) {
                String line = sumLoadList.get(i) + "," + sumPowerList.get(i) + "," + sumPriceList.get(i) + ",";
                writer.write(line);
                writer.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("统计数据文件写入失败: " + e.getMessage());
        }

        System.out.println("当前模式为：" + optimizationModel_day);
        System.out.println("模拟时刻总冷量为：" + sumLoad / 1000.0 + "MWh");
        System.out.println("模拟时刻总耗电量为：" + sumPower / 1000.0 + "MWh");
        System.out.println("模拟时刻总费用为：" + sumPrice / 10000.0 + "万元");
        System.out.println("模拟时刻总平均电价为：" + sumPrice / sumPower + "元/kWh");
        System.out.println("模拟时刻全冰蓄冷系统总平均EER为：" + sumEER);
        System.out.println("模拟时刻常规制冷系统总平均EER为：" + sumEER_CBLRS);
        System.out.println("模拟时刻冰槽循环系统总平均EER为：" + sumEER_ISS);
        System.out.println("模拟时刻基载机组总平均COP为：" + sumCOP_BLRU);
        System.out.println("模拟时刻双工况机组制冷工况总平均COP为：" + sumCOPDOCUCooling);
        System.out.println("模拟时刻双工况机组制冰工况总平均COP为：" + sumCOPDOCUIceMaking);
        System.out.println("计算全部完成");
        String folderPath = "simulatedData/file_csv";
        String outputCSVPath = "consolidatedData/consolidatedCalculatedData.csv";

        try (Stream<Path> pathStream = Files.walk(Paths.get(folderPath))) {
            List<Path> csvFiles = pathStream.filter((path) -> !Files.isDirectory(path))
                    .filter((path) -> path.toString().endsWith(".csv"))
                    .sorted(Comparator.naturalOrder())
                    .collect(Collectors.toList());
            if (csvFiles.isEmpty()) {
                throw new IllegalArgumentException("文件夹中没有CSV文件！");
            }

            try (BufferedWriter writer = new BufferedWriter(new FileWriter(outputCSVPath))) {
                boolean isHeaderWritten = false;

                for (Path file : csvFiles) {
                    String line;
                    try (BufferedReader reader = Files.newBufferedReader(file)) {
                        while ((line = reader.readLine()) != null) {
                            if (!isHeaderWritten) {
                                writer.write(line);
                                writer.newLine();
                                isHeaderWritten = true;
                            }
                        }
                    }

                    try (BufferedReader reader = Files.newBufferedReader(file)) {
                        boolean skipHeader = true;

                        while ((line = reader.readLine()) != null) {
                            if (skipHeader) {
                                skipHeader = false;
                            } else {
                                writer.write(line);
                                writer.newLine();
                            }
                        }
                    }
                }
            }

            System.out.println("合并成功！输出文件路径：" + outputCSVPath);
        } catch (IOException e) {
            System.err.println("文件操作发生异常：");
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            System.err.println(e.getMessage());
        }

        System.out.println("文件输出全部完成");
    }
}
